//
//  GiftLayout.swift
//  WalkNovel
//
//  Created by Kevin on 2023/9/18.
//

import UIKit

class GiftLayout: UICollectionViewFlowLayout {
    
    var columnPerPage : Int = 4
    var rowsPerPage   : Int = 2
    var itemSpaceHor  : CGFloat = 0
    var itemSpaceVer  : CGFloat = 0
    fileprivate var attrArray     : [UICollectionViewLayoutAttributes] = []
    
    fileprivate var cellItemSize  : CGSize = .zero
    
    fileprivate var totalItemCount : Int {
        return self.collectionView?.numberOfItems(inSection: 0) ?? 0
    }
    
    fileprivate var totalCountPerPage : Int {
        return self.columnPerPage * self.rowsPerPage
    }
        
    fileprivate var collectionViewSize : CGSize {
        return self.collectionView?.frame.size ?? .zero
    }
    
    override func prepare() {
        
        let itemWidth  = (collectionViewSize.width - (CGFloat(columnPerPage + 1)) * itemSpaceHor) / CGFloat(columnPerPage)
        let itemHeight = (collectionViewSize.height - (CGFloat(rowsPerPage + 1)) * itemSpaceVer) / CGFloat(rowsPerPage)
        self.cellItemSize = CGSize(width: itemWidth, height: itemHeight)
        self.attrArray.removeAll()
        for i in 0 ..< self.totalItemCount {
            let indexPath = IndexPath(item: i, section: 0)
            if let attr = self.layoutAttributesForItem(at: indexPath) {
                attrArray.append(attr)
            }
        }
    }
    
    override func layoutAttributesForItem(at indexPath: IndexPath) -> UICollectionViewLayoutAttributes? {
        let item       = indexPath.item + 1 //索引位置从1开始
        let pageNum    = item / totalCountPerPage
        let itemInPage = item % totalCountPerPage
        let remainder  = itemInPage % columnPerPage
        let isLastItemInPage = item % totalCountPerPage == 0 // 是否是当前页的最后一个cell
        var x = CGFloat(pageNum) * collectionViewSize.width
        if isLastItemInPage {
            x -= (itemSpaceHor + cellItemSize.width)
        } else if remainder == 0 {
            x += CGFloat(columnPerPage) * itemSpaceHor + CGFloat(columnPerPage - 1) * cellItemSize.width
        } else {
            x += CGFloat(remainder) * itemSpaceHor + CGFloat(remainder - 1) * cellItemSize.width
        }
        
        var rowInPage = item / columnPerPage - pageNum * rowsPerPage
        if (item - pageNum * totalCountPerPage) % columnPerPage > 0 {
            rowInPage += 1
        }
        var y : CGFloat = 0
        if isLastItemInPage {
            y = collectionViewSize.height - itemSpaceVer - cellItemSize.height
        } else {
            y = CGFloat(rowInPage) * itemSpaceVer + CGFloat(rowInPage - 1) * cellItemSize.height
        }
        let layoutAttr = super.layoutAttributesForItem(at: indexPath)
        layoutAttr?.frame = CGRect(origin: CGPoint(x: x, y: y), size: self.cellItemSize)
        return layoutAttr
    }
    
    override var collectionViewContentSize: CGSize {
        let remainder = totalItemCount % (columnPerPage * rowsPerPage)
        var pageNum = totalItemCount / (columnPerPage * rowsPerPage)
        if remainder > 0 {
            pageNum += 1
        }
        return CGSize(width: CGFloat(pageNum) * collectionViewSize.width,
                      height: collectionViewSize.height)
    }
    
    override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
        return self.attrArray
    }
}
